import axios from 'axios'
import store from '@/store'

import {
    getCookieValue
} from '@/utils/auth'

console.log(process.env)

// 创建axios实例
const service = axios.create({
    // withCredentials: true,
    // baseURL: process.env.BASE_API, // api 的 base_url
    baseURL: process.env.BASE_API, // api 的 base_url
    timeout: 120000 // 请求超时时间
})

// request拦截器
service.interceptors.request.use(
    config => {
        if (store.getters.token) {
            config.headers[ 'Authorization' ] = getCookieValue("username") // 让每个请求携带自定义token 请根据实际情况自行修改
        }
        if (!config.noLoading) { //不需要 loading
            // store.commit("loading", { loading: true });
        }
        // if (1) {
        //   config.headers['X-Token'] = getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
        // }
        return config
    },
    error => {
        // Do something with request error
        // store.commit("loading", { loading: false });
        console.log(error) // for debug
        Promise.reject(error)
    }
)

// response 拦截器
service.interceptors.response.use(
    response => {
        const res = response.data
        // setTimeout(() => {
        // store.commit("loading", { loading: false });
        // }, 300)
        return res
    },
    error => {
        console.log('err' + error) // for debug

        // store.commit("loading", { loading: false });
        return Promise.reject(error)
    }
)

export default service
